package com.naughty.userlogin02.controller;

import com.alibaba.fastjson.JSON;
import com.naughty.userlogin02.bean.Administrator;
import com.naughty.userlogin02.bean.QueryInfo;
import com.naughty.userlogin02.dao.AdministratorDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class AdministratorController {

    @Autowired
    AdministratorDao administratorDao;

    @CrossOrigin
    @RequestMapping("/allAdministrator")
    public String getAdministratorList(QueryInfo queryInfo) {

        System.out.println("hhhhhhhhhhhh");
        System.out.println(queryInfo);
        int numbers = administratorDao.getAdministratorCounts("%" + queryInfo.getQuery() + "%");// 获取数据总数
        System.out.println("aaaaaa");
        System.out.println(numbers);

        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        List<Administrator> users = administratorDao.getAllAdmin("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers", numbers);
        res.put("data", users);
        System.out.println("总条数：" + numbers);
        String users_json = JSON.toJSONString(res);
        System.out.println("pppppp");
        System.out.println(users_json);
        return users_json;
    }

    @RequestMapping("/addAdministrator")
    public String addAdministrator(@RequestBody Administrator administrator) {
        System.out.println("bbbbbbbbbbbb");
        System.out.println(administrator);
//        administrator.setRole("普通用户");
//        administrator.setSpwd("12345");
        int i = administratorDao.addAdministrator(administrator);
        String str = i > 0 ? "success" : "error";
        System.out.println("bbaaaa");
        System.out.println(i);
        return str;//也可以通过这里返回报错
    }

    @RequestMapping("/editAdministrator")
    public String editAdministrator(@RequestBody Administrator administrator) {
        System.out.println(administrator);
        int i = administratorDao.editAdministrator(administrator);
        String str = i > 0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/delete2(Id)")
    public String deleteAdministrator(String aid) {
        System.out.println(aid);
        int i = administratorDao.deleteAdministrator(aid);
        String str = i > 0 ? "success" : "error";
        return str;
    }
}
